package it.storm.solution;

/**
 * 495. 提莫攻击
 * https://leetcode-cn.com/problems/teemo-attacking/
 */
public class Solutions_495 {
    public static void main(String[] args) {
        int[] timeSeries = {1, 4};
        int duration = 2;  // output: 4

//        int[] timeSeries = {1, 2};
//        int duration = 2;  // output: 3

//        int[] timeSeries = {1, 2};
//        int duration = 3;  // output: 4

        int result = findPoisonedDuration(timeSeries, duration);
        System.out.println(result);
    }

    public static int findPoisonedDuration(int[] timeSeries, int duration) {
        if (timeSeries == null || timeSeries.length < 1 || duration <= 0) {
            // 攻击时间为 0 的情况
            return 0;
        }
        // res：结果，end：结束区间
        int res = 0, end = 0;
        for (int t : timeSeries) {
            if (t >= end) {
                res += duration;
            } else {
                // 计算出时间点 t，攻击持续 duration 后，且持续时间超出 end 的区间
                // 因为 end 区间内的时间已经计算过
                res += t + duration - end;
            }
            // 更新结束区间
            end = t + duration;
        }
        return res;
    }
}
